import 'package:equatable/equatable.dart';

/// 评论模型
/// 使用Equatable实现相等性比较
class CommentModel extends Equatable {
  /// 评论ID
  final String id;

  /// 评论用户ID
  final String userId;

  /// 帖子ID
  final String postId;

  /// 评论内容
  final String content;

  /// 评论时间
  final DateTime commentTime;

  /// 点赞数量
  final int likeCount;

  /// 点踩数量
  final int dislikeCount;

  const CommentModel({
    required this.id,
    required this.userId,
    required this.postId,
    required this.content,
    required this.commentTime,
    this.likeCount = 0,
    this.dislikeCount = 0,
  });

  /// 从JSON创建评论对象
  factory CommentModel.fromJson(Map<String, dynamic> json) {
    return CommentModel(
      id: json['id'] as String,
      userId: json['userId'] as String,
      postId: json['postId'] as String,
      content: json['content'] as String,
      commentTime: DateTime.parse(json['commentTime'] as String),
      likeCount: json['likeCount'] as int? ?? 0,
      dislikeCount: json['dislikeCount'] as int? ?? 0,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'postId': postId,
      'content': content,
      'commentTime': commentTime.toIso8601String(),
      'likeCount': likeCount,
      'dislikeCount': dislikeCount,
    };
  }

  /// 复制并修改部分属性
  CommentModel copyWith({
    String? id,
    String? userId,
    String? postId,
    String? content,
    DateTime? commentTime,
    int? likeCount,
    int? dislikeCount,
  }) {
    return CommentModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      postId: postId ?? this.postId,
      content: content ?? this.content,
      commentTime: commentTime ?? this.commentTime,
      likeCount: likeCount ?? this.likeCount,
      dislikeCount: dislikeCount ?? this.dislikeCount,
    );
  }

  /// 验证评论内容是否有效
  bool get isValid => content.trim().isNotEmpty;

  @override
  List<Object?> get props => [
    id,
    userId,
    postId,
    content,
    commentTime,
    likeCount,
    dislikeCount,
  ];

  @override
  String toString() {
    return 'CommentModel(id: $id, userId: $userId, postId: $postId, content: $content, commentTime: $commentTime, likeCount: $likeCount, dislikeCount: $dislikeCount)';
  }
}
